package com.ww.learn.coroutine.context

import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking

fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")

// 打印结果
//[main @coroutine#1] Started main coroutine
//[main @v1coroutine#2] Computing v1
//[main @v2coroutine#3] Computing v2
//[main @coroutine#1] The answer for v1 * v2 = 42
fun main() = runBlocking {
    log("Started main coroutine")
    val v1 = async(CoroutineName("v1coroutine")) {
        delay(500)
        log("Computing v1")
        6
    }
    val v2 = async(CoroutineName("v2coroutine")) {
        delay(1000)
        log("Computing v2")
        7
    }
    log("The answer for v1 * v2 = ${v1.await() * v2.await()}")
}